from input import Input
from plugin_system import Plugin

import numpy as np

class PlanetParametersInput(Input):
    def __init__(self, filename=None):
        self.filename = filename
        self.fl = None
        
    def initialize(self):
        self.fl = open(self.filename)

    def finalize(self):
        self.fl.close()

    def read(self, system):
        # we assume a file format where each object's data is on one line
        # the first column is the object's name while the second column
        # is the object's mass multiplied by G.
        mass_g = {}
        for ln in self.fl:
            cols = ln.split()
            name = cols[0]
            mass_g[name] = np.float64(cols[1])
        G = mass_g["Sun"]
        for name, mass in mass_g.iteritems():
            system.planet_names.append(name)
            system.masses[name] = mass / G

class PlanetParametersInputPluginAdapter(Plugin, PlanetParametersInput):
    plugin_name = "PlanetParametersInput"
    parameter_definitions = {"filename" : (str, True, "File from which to read data.")}
    
    def __init__(self, *args, **user_params):
        params = self.configure_parameters(**user_params)
        PlanetParametersInput.__init__(self, filename=params["filename"])
        
